//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    listTag:[],
    status:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var openid = wx.getStorageSync('openid')
    if(openid){
      wx.request({
        url: 'https://carplus.qingmengcar.com/user/index/index',
        data:{
          retype:'wxapp',
          openid:openid
        },
        method:'post',
        success:function(d){
          if (d.data.code == -1) {
            if(d.data.data.flag==0){
              wx.navigateTo({
                url: '../../message/message?msg='+d.data.msg+'&success='+1,
              })
            }else{
              //获取用户信息成功
              wx.navigateTo({
                url: '../../pages/login/login',
              })
            }
          }
        },
        fail:function(res){}
      })
    }else{
      wx.login({
        success (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: 'https://carplus.qingmengcar.com/user/index/index',
              data: {
                retype:'wxapp',
                code: res.code
              },
              success:function(d){
                if(d.data.code == 0){
                  wx.setStorage({
                    data: d.data.openid,
                    key: 'openid',
                  })
                }else{
                  if(d.data.data.flag==0){
                    wx.navigateTo({
                      url: '../../message/message?msg='+d.data.msg+'&success='+1,
                    })
                  }else{
                    wx.setStorage({
                      data: d.data.openid,
                      key: 'openid',
                    })
                    wx.navigateTo({
                      url: '../../pages/login/login',
                    })
                  }
                }
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    }
    let that = this
    wx.request({
      url: 'https://carplus.qingmengcar.com/user/index/getcategory',
      method:'POST',
      success:function(res){
        if(res.data.code == 0){
          that.setData({
            listTag:res.data.data
          })
        }        
      }
    })
    that.checkNumber()
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  toList: function(e){
    var typeId =  e.currentTarget.dataset['id'];
    wx.navigateTo({
      url: '../list/list?cateid='+typeId
    })
  },
  getCateType: function(e){
    
  },
  checkNumber:function(openid){
    let that = this
    wx.request({
      url: 'https://carplus.qingmengcar.com/user/index/getMemberType',
      method:'POST',
      data:{
        openid:openid
      },
      success:function(res){
        if(res.data.code==0){
          that.setData({
            status:res.data.data.status
          })
        }
      }
    })
  }
})
